<template>
  <div class="container">
    <!-- 新增学科会话框 -->
    <el-dialog
      :title="titleInfo.text + titleInfo.pageTitle"
      :visible.sync="dialogVisible"
      width="400px"
      @close="$emit('close')"
    >
      <!-- 弹框内容主体区 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="dataForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="学科名称" prop="subjectName" label-width="80px">
          <el-input
            v-model="ruleForm.subjectName"
            placeholder="请输入学科名称"
            size="small"
          ></el-input>
        </el-form-item>

        <el-form-item label="是否显示" label-width="80px">
          <el-switch
            v-model="ruleForm.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <!-- 底部按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="closed">取消</el-button>
        <el-button type="primary" @click="AddSubjects">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects'

export default {
  name: 'SubjectsAdd',
  props: {
    titleInfo: {
      type: Object,
      required: true
    },
    // 接收父亲传过来的值
    formBase: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      // 添加用户的表单数据
      ruleForm: {
        id: null,
        // 学科名称
        subjectName: '',
        // 是否在页面显示
        // 控制开关按钮
        isFrontDisplay: 1
      },
      // 表单校验规则
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      },
      // 控制新增学科会话框
      dialogVisible: false
    }
  },

  methods: {
    // 弹层显示
    open() {
      this.$emit('open')
      this.dialogVisible = true
    },

    // 弹层隐藏
    closed() {
      this.dialogVisible = false
    },

    // 新增、修改学科
    AddSubjects() {
      // validate 用来做表单验证
      this.$refs.dataForm.validate(async val => {
        if (val) {
          // 有数据 添加成功则 打印 true，反之打印 false
          console.log(val)
          // 隐藏新增学科会话框
          this.closed()
          // 根据id判断下是增加学科还是编辑学科
          if (this.formBase.id) {
            await update(this.ruleForm).then(() => {
              this.$emit('newSub')
            })
            this.$message.success('修改学科成功')
          } else {
            this.ruleForm.id = null
            await add(this.ruleForm).then(() => {
              this.$emit('newSub')
            })
            this.$message.success('新增学科成功')
          }
        } else {
          this.$message.error('*号为必填项！')
        }
        this.dialogVisible = false
      })
    }
  },
  watch: {
    formBase: {
      handler() {
        // 把父组件的值传给子组件
        this.ruleForm.id = this.formBase.id
        this.ruleForm.subjectName = this.formBase.subjectName
        this.ruleForm.isFrontDisplay = this.formBase.isFrontDisplay
        if (this.formBase.isFrontDisplay === undefined) {
          this.ruleForm.isFrontDisplay = 1
        }
      },
      immediate: true
    }
  }
}
</script>

<style scoped>
.el-form-item--medium {
  margin-bottom: 18px;
}
</style>
